/* smog_residual_trends.do: This program plots the residuals of air pollution
after controlling for county and time fixed effects and various degrees of 
county-specific trends, with the aim of demonstrating that cubic trends make
sense in our main regressions*/
capture log close
clear all

cd ${path}appendix

use ../analysis/pollution_star_merged

gen week = wofd(date)
format date %td
xtset county date
/* Define regression conditions*/
local conditions  if year>=1998 & inlist(county,5,12,18,22,26,53)==0 & (inlist(county,3,6,11,12,13,17,23,47,58)==0 | (year !=2002&year!=2003)) 

/* Loop over co and NOx*/
foreach pollutant in co nox{

	replace `pollutant' = `pollutant'*1000

	if "`pollutant'" == "co" local bound = 200
	else local bound = 8
	local inc = `bound'/2

	/* First with just fixed effects*/
	capture drop residuals
	qui regress `pollutant' i.county i.week `conditions' 
	disp "No Trends"
	qui predict residuals, res
	

	
	
	#delimit;

/* Only county and week FE */

	twoway lowess residuals date if county == 30, lpattern(solid) ||  lowess residuals date if county == 1, lpattern(dash)|| 
	lowess residuals date if county == 34, lpattern(longdash) ||  lowess residuals date if county == 37, lpattern(dash_dot)|| 
	if e(sample), tlabel(01jan1998(365)01jan2010, format(%tdCCYY) angle(vertical)) ylabel(-`bound'(`inc')`bound') 
	legend(col(2) label(1 Orange) label(2 Alameda) label(3 Sacramento) label(4 San Diego))
	graphregion(color(white)) xtitle(County) ytitle(Residual Pollution Reading)  ;
	
	graph export `pollutant'_res_fe.pdf, replace;

/* County, week and county-year FE */
	
	capture drop residuals;
	
	qui regress `pollutant' i.county i.week i.county#i.year `conditions'; 

	predict residuals, res;

	twoway lowess residuals date if county == 30, lpattern(solid) ||  lowess residuals date if county == 1, lpattern(dash)|| 
	lowess residuals date if county == 34, lpattern(longdash) ||  lowess residuals date if county == 37, lpattern(dash_dot)|| 
	if e(sample), tlabel(01jan1998(365)01jan2010, format(%tdCCYY) angle(vertical)) ylabel(-`bound'(`inc')`bound') 
	legend(col(2) label(1 Orange) label(2 Alameda) label(3 Sacramento) label(4 San Diego))
	graphregion(color(white))  xtitle(County) ytitle(Residual Pollution Reading)   ;
	
	graph export `pollutant'_res_cyfe.pdf, replace;

	
	#delimit cr
}


